package io.github.talelin.latticy.controller.api;

import io.github.talelin.core.annotation.AdminRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.bo.PageCounterBO;
import io.github.talelin.latticy.common.CommonUtil;
import io.github.talelin.latticy.dto.ProjectDTO;
import io.github.talelin.latticy.entity.Project;
import io.github.talelin.latticy.service.ProjectService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;

@RestController
@RequestMapping("/api/project")
@Validated
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 创建项目
     */
    @PostMapping()
    @LoginRequired
    public CreatedVO create(@RequestBody @Validated ProjectDTO dto) {
        projectService.create(dto);
        return new CreatedVO<>();
    }

    /**
     * 编辑项目
     */
    @PutMapping("/{id}")
    @LoginRequired
    public UpdatedVO update(@PathVariable @Positive Long id,
                            @RequestBody @Validated ProjectDTO dto) {
        projectService.edit(id, dto);
        return new UpdatedVO<>();
    }

    /**
     * 删除项目
     */
//    @DeleteMapping("/{id}")
//    @AdminRequired
//    public DeletedVO delete(@PathVariable @Positive Long id) {
//        projectService.delete(id);
//        return new DeletedVO<>();
//    }

    /**
     * 获取项目列表
     */
    @GetMapping("/page")
    @LoginRequired
    public PageResponseVO<Project> page(@RequestParam(defaultValue = "0") Integer start,
                              @RequestParam(defaultValue = "20") Integer count,
                              @RequestParam(defaultValue = "0") String type,
                              @RequestParam(defaultValue = "0") String status,
                              @RequestParam(name = "is_select", defaultValue = "true") Boolean isSelect) {
        PageCounterBO pageCounter = CommonUtil.converToPageParamter(start, count);
        Page<Project> paging = projectService.getAll(isSelect, type, status, pageCounter.getPage(), pageCounter.getSize());
        return new PageResponseVO<>(paging.getTotalElements(), paging.getContent(), paging.getNumber(), paging.getSize());
    }

    /**
     * 项目 -- 测试
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Project get(@PathVariable Long id) {
        return projectService.get(id);
    }
}
